package com.viking.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/add")
    public String add() {
        System.out.println("下单成功！");
        String result = restTemplate.getForObject("http://stock-service:8021/stock/reduce", String.class);
        return "ok,下单成功！库存服务调用返回：" + result;
    }

    @RequestMapping("/header")
    public String header(@RequestHeader("X-Request-color") String color) {
        return color;
    }


    @RequestMapping("/flow")
    public String flow() {
        return "正常访问";
    }


    @RequestMapping("/flowThread")
    public String flowThread() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        System.out.println("正常访问");
        return "正常访问";
    }


    @RequestMapping("/get")
    public String get() {
        return "查询订单";
    }


    @RequestMapping("/err")
    public String err() {
        int a = 1 / 0;
        return "hello";
    }

    @RequestMapping("/get/{id}")
    public String getById(@PathVariable("id") Integer id) {
        System.out.println("正常访问");
        return "正常访问";
    }


}
